<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Products
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Products extends CI_Controller {
	var $cat = '';
	var $q = '';
	var $copyid = '';
	var $limit = '';
	var $orderby = '';
	var $ordermode = '';
	var $updated = '';
	var $failed = '';
	var $error = '';
	var $name = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $site_url = '';

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Pro');
		$this->cat = $GLOBALS['var']['filter_cat'];
		$this->q = $this->input->get('q', true);
		$this->copyid = $this->input->get('copyid', true);
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->error = $this->input->get('error', true);
		$this->name = $this->input->get('name', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'],
			'cat' => $this->cat,
			'q' => $this->q,
			'copyid' => $this->copyid,
			'limit' => $this->limit,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
		$this->site_url = site_url($GLOBALS['var']['act']);
	}

	public function index()
	{
		/*
		* Huy tham so copy trong tham so url
		*/
		$this->uri_arr['copyid'] = '';
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->Pro->show($this->uri_arr, true);
		$page_list = page_list($num_rows, $this->uri_arr);
		$cat_list = $this->Pro->filter_category_list();
		$data = array(
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'uri_str' => $this->uri_str,
			'products' => array(),
			'page_list' => $page_list,
			'cat_list' => $cat_list,
			'filter_cat' => $GLOBALS['var']['filter_cat'],
			'filter_category_list' => $this->Pro->filter_category_list(),
			'page_list' => page_list($num_rows, $this->uri_arr)
		);
		$rows = $this->Pro->show($this->uri_arr);
		if ($rows) $data['products'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Danh sách sản phẩm',
			'add_link' => current_url().'/update',
			'search' => true,
			'page_list' => $page_list,
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => $cat_list,
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function update($id = '')
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		if ($this->copyid && !$id)
		{
			$info = $this->Pro->info($this->copyid);
			$info['img'] = '';
			$info['img_slider'] = '';
			$info['images'] = '';
			$info['code'] = randomkeys(8);
		}
		else
		{
			$info = $this->Pro->info($id);
		}
		if (!$id) $info['code'] = randomkeys(8);
		$data = array(
			'info' => $info,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'error' => $this->error,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,

		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => ($id ? 'Cập nhập' : 'Thêm mới').' sản phẩm',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		$name_vn = $this->input->post('name_vn', true);
		/*
		* Them tham so url
		*/
		if ($this->copyid)
		{
			$id = '';
		}
		else
		{
			$id = $this->input->post('id', true);
		}
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Kiem tra token va tham so yeu cau
		*/
		if (!token_validation() || $name_vn == '')
		{
			redirect($GLOBALS['var']['act'].'/update'.($id ? '/'.$id : '').url_uri($this->uri_arr));
		}
		/*
		* Xu ly du lieu
		*/
		$name_en = $this->input->post('name_en', true);
		if($name_en == '') $name_en = $name_vn;
		$alt_vn = $this->input->post('alt_vn', true);
		$alt_en = $this->input->post('alt_en', true);
		if($alt_en == '') $alt_en = $alt_vn;
		$cat = $this->input->post('cat', true);
		$data = array(
			'name_vn' => $name_vn,
			'name_en' => $name_en,
	        'cat' => (is_array($cat) ? json_encode($cat) : $cat),
	        'keyword' => url_title(viet_decode($name_vn), '-', true),

	        'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($this->Pro->process($data, $id))
		{
			$this->uri_arr['updated'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		$this->uri_arr['copyid'] = '';
		if ($id > 0)
		{
			redirect($GLOBALS['var']['act'].'/update/'.$id.url_uri($this->uri_arr));
		}
		else
		{
			redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
		}
	}

}

/* End of file products.php */
/* Location: ./application/controllers/products.php */